package com.tyjt.common.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author 种鑫
 * @date 2024/7/22 13:46
 */
@Component
@Slf4j
public class EmqxUtil {

    private static MqttClient mqttClient;

    @Autowired
    private EmqxUtil(MqttClient mqttClient) {
        EmqxUtil.mqttClient = mqttClient;
    }


    public static void sendMsg(String topic, byte[] msg) {
        if (mqttClient.isConnected()) {
            try {
                MqttMessage mqttMessage = new MqttMessage(msg);
                mqttMessage.setQos(0);
                //  订阅数据
                mqttClient.publish(topic, mqttMessage);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }
}
